//
// 采购退货明细
//
<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          :cell-class-name="cellClassName"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          row-key="orderByNumber"
          :cell-style="cellStyle"
          :summary-method="getSummaries"
          :show-summary="true"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" :align="column.align" />
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { list, listForExport } from '@/api/purchaseManage/purchaseStatistics/purchaseReturnDetail'
import { getDictSelects } from '@/api/common/common'
import { getInfo } from '@/api/purchaseManage/purchaseReturn'
import { getInfo as rejectsDisposeInfo } from '@/api/qualityManage/rejectsDispose'
import { getInfo as OrderCodeInfo } from '@/api/purchaseManage/purchaseOrder'
import ShowDetail from '../../purchaseReturn/showPurchaseReturn'
import ShowDetailDisposalCode from '@/components/JLINK2/common/ShowDetailTitle'
import ShowDetailOrder from '../../purchaseOrder/showPurchaseOrder'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '采购退货明细', // 页面唯一标识
      pageExportId: '采购退货明细表导出',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'returnTime',
        isAsc: 'desc'
      },
      totalWord: ['purchaseNumber', 'returnNumber'], // 合计导出字段
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'code', label: '退货单号', minWidth: '180', isRegular: true },
        { prop: 'returnName', label: '退货人员', minWidth: '80', canOrder: false, isRegular: true },
        { prop: 'returnTime', label: '退货日期', minWidth: '140', isRegular: true },
        { prop: 'defectiveProductDisposalCode', label: '不良品处置单', minWidth: '130', isRegular: true },
        { prop: 'purchaseOrderCode', label: '采购订单号', minWidth: '130', canOrder: false, isRegular: true },
        { prop: 'supplierName', label: '供应商名称', minWidth: '100', canOrder: false, isRegular: true },
        { prop: 'returnReason', label: '退货原因', minWidth: '200', canOrder: false },
        { prop: 'remark', label: '单据备注', minWidth: '200', canOrder: false },
        { prop: 'materialCode', label: '物料编码', minWidth: '130', canOrder: false },
        { prop: 'materialName', label: '物料名称', minWidth: '100', canOrder: false },
        { prop: 'materialModel', label: '规格型号', minWidth: '130', canOrder: false, isShow: false },
        { prop: 'materialUnitLabel', label: '物料单位', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false, isShow: false },
        { prop: 'purchaseNumber', label: '采购数量', align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'returnNumber', label: '退货数量', align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'detailsRemark', label: '备注', minWidth: '200', canOrder: false }
      ],
      exportTableColumns: [
        { prop: 'code', label: '退货单号', minWidth: '130' },
        { prop: 'returnName', label: '退货人员', minWidth: '80', canOrder: false },
        { prop: 'returnTime', label: '退货日期', minWidth: '140' },
        { prop: 'defectiveProductDisposalCode', label: '不良品处置单', minWidth: '130' },
        { prop: 'purchaseOrderCode', label: '采购订单号', minWidth: '130', canOrder: false },
        { prop: 'supplierName', label: '供应商名称', minWidth: '100', canOrder: false },
        { prop: 'returnReason', label: '退货原因', minWidth: '200', canOrder: false },
        { prop: 'remark', label: '单据备注', minWidth: '200', canOrder: false },
        { prop: 'materialCode', label: '物料编码', minWidth: '130', canOrder: false },
        { prop: 'materialName', label: '物料名称', minWidth: '100', canOrder: false },
        { prop: 'materialModel', label: '规格型号', minWidth: '130', canOrder: false },
        { prop: 'materialUnitLabel', label: '物料单位', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'purchaseNumber', label: '采购数量', align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'returnNumber', label: '退货数量', align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'detailsRemark', label: '备注', minWidth: '200', canOrder: false }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'code', label: '退货单号', type: 'input' },
        { prop: 'returnName', label: '退货人员', type: 'input' },
        { prop: 'returnTime', label: '退货日期', clearable: false, type: 'time', isTime: true, hasDefault: true, defaultKey: 'returnTimeDefault' },
        { prop: 'defectiveProductDisposalCode', label: '不良品处置单', type: 'input' },
        { prop: 'purchaseOrderCode', label: '采购订单号', type: 'input' },
        { prop: 'supplierName', label: '供应商名称', type: 'input' },
        { prop: 'materialCode', label: '物料编码', type: 'input' },
        { prop: 'materialName', label: '物料名称', type: 'input' },
        { prop: 'materialModel', label: '规格型号', type: 'input', isShow: false },
        { prop: 'materialUnit', label: '物料单位', type: 'dict', optionsName: 'equipmentList1', isShow: false }
      ],
      returnTimeDefault: [],
      operationData: [ // 右上角的操作按钮
        { label: '批量导出3', perm: '' },
        { label: '页面配置2', perm: '' }
      ],
      // statusOptions: [
      //   { value: undefined, label: '全部' },
      //   { value: '1', label: '采购中' },
      //   { value: '2', label: '已完成' }
      // ],
      // statusOptions2: [],
      equipmentList1: [],
      showKey: [ // 查看详情的配置（根据配置自动生成查看页面）
        {
          name: '基本信息',
          columns: [
            { type: 'form', name: '退货单号', key: 'code' }, // type: 'form' 表示是普通的表单展示
            { type: 'form', name: '退货人员', key: 'returnName' },
            { type: 'form', name: '不良品处置单', key: 'defectiveProductDisposalCode' },
            { type: 'form', name: '采购订单号', key: 'purchaseOrderCode' }, // length:'double' 表示占两列
            { type: 'form', name: '采购供应商', key: 'supplierName' }, // type: 'form' 表示是普通的表单展示
            { type: 'form', name: '退货原因', key: 'returnReason' },
            { type: 'form', name: '退货日期', key: 'returnTime' },
            { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
            { type: 'form', name: '备注', key: 'remark', length: 'double' },
            {
              type: 'table', name: '', key: 'vos', columns: [ // type: 'table' 表示是表格
                { type: 'form', name: '物料编码', key: 'materialCode', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
                { type: 'form', name: '物料名称', key: 'materialName', minWidth: '100' },
                { type: 'form', name: '规格型号', key: 'materialModel', minWidth: '100' },
                { type: 'form', name: '物料单位', key: 'materialUnitLabel', minWidth: '100' },
                { type: 'form', name: '采购数量', key: 'purchaseNumber', Width: this.SETTINGS.settings_decimalDigits_length }, // type: 'form' 表示是普通的表单展示
                { type: 'form', name: '不良数量', key: 'disqualificationNumber', Width: this.SETTINGS.settings_decimalDigits_length },
                { type: 'form', name: '退货数量', key: 'returnNumber', fixed: 'right', Width: this.SETTINGS.settings_decimalDigits_length },
                { type: 'form', name: '备注', key: 'remark', minWidth: '200' } // type: 'form' 表示是普通的表单展示
              ]
            }
          ]
        },
        {
          name: '供应商信息',
          columns: [
            { type: 'form', name: '交货方式', key: 'deliveryTypeLabel' }, // type: 'form' 表示是普通的表单展示
            { type: 'form', name: '收货人', key: 'consignee' },
            { type: 'form', name: '联系方式', key: 'contract' },
            { type: 'form', name: '注意事项', key: 'announcements' },
            { type: 'form', name: '收货地址', key: 'receiveAddress', length: 'double' }
          ]
        }
      ],
      showDisposalCodeKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '不良品单号', key: 'code' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '处理人员', key: 'disposeName' },
          { type: 'form', name: '处理日期', key: 'disposeDate' },
          { type: 'form', name: '不良品来源', key: 'sourceLabel' },
          { type: 'form', name: '关联表单', key: 'relatedCheckCode' }, // length:'double' 表示占两列
          { type: 'form', name: '物料编码', key: 'materialCode' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '物料名称', key: 'materialName' },
          { type: 'form', name: '规格型号', key: 'materialModel' },
          { type: 'form', name: '物料单位', key: 'materialUnitName' },
          { type: 'form', name: '不良数量', key: 'disqualificationNumber' },
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '不良处理', key: 'data', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '处置方式', key: 'disposeWayLabel' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '处置数量', key: 'disposeNumber' }
            ]
          }
        ]
      },
      OrdershowKey: {
        type: 'column_two',
        name: '基本信息',
        keys: [
          { type: 'form', name: '采购订单号', key: 'code' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '采购人员', key: 'orderName' },
          { type: 'form', name: '采购日期', key: 'orderTime' },
          { type: 'form', name: '供应商名称', key: 'supplierName' },
          { type: 'form', name: '总数量', key: 'totalNumber' }, // length:'double' 表示占两列
          { type: 'form', name: '含税总金额', key: 'taxTotalMoneyThousandths' },
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '', key: 'detailList', columns: [ // type: 'table' 表示是表格// type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitLabel', minWidth: '100' },
              { type: 'column', name: '采购数量', key: 'purchaseNumber', minWidth: this.SETTINGS.settings_decimalDigits_length, fixed: 'right', align: 'right' },
              { type: 'column', name: '含税单价', key: 'taxPriceThousandths', minWidth: this.SETTINGS.settings_decimalDigits_length }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '税率%', key: 'taxRateLabel', minWidth: '100' },
              { type: 'column', name: '含税总金额', key: 'totalTaxPriceThousandths', minWidth: this.SETTINGS.settings_decimalDigits_length, align: 'right' },
              { type: 'column', name: '采购交期', key: 'purchaseDeliveryTime', minWidth: '140' },
              { type: 'column', name: '备注', key: 'remark', minWidth: '200' }
            ]
          }
        ]
      },
      totalList: {}
    }
  },
  watch: {
    tableData() {
      this.$nextTick(() => {
        for (const elementNodeListOfElement of document.querySelectorAll('.SaleOrderCodeColumnClass')) {
          if (!elementNodeListOfElement.childNodes[0].childNodes[0].className || elementNodeListOfElement.childNodes[0].childNodes[0].className == 'commonTableColumn') {
            const paddingNode = document.createElement('div')
            paddingNode.className = 'SaleOrderCodeColumnClass_expand'
            paddingNode.innerHTML = ''
            elementNodeListOfElement.childNodes[0].insertBefore(paddingNode, elementNodeListOfElement.childNodes[0].childNodes[0])
          }
        }
      })
    }
  },
  created() {
    // 获取当前日期函数
    this.returnTimeDefault = [this.$moment().startOf('month').format('YYYY-MM-DD') + ' 00:00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23:59']
  },
  mounted() {
    // this.getEquipmentStatus()
    this.getEquipmentStatusUnit()
  },
  methods: {
    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 1) {
        return 'SaleOrderCodeColumnClass'
      }
      if (!row.isNotselected) {
        return 'displayNoneClass'
      }
    },
    cellStyle({ row, column }) {
      if (row.code && column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
      if (row.defectiveProductDisposalCode && column.property == 'defectiveProductDisposalCode') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
      if (row.purchaseOrderCode && column.property == 'purchaseOrderCode') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          if (row.code) {
            this.showDetail(row)
          }
          break
        case 'defectiveProductDisposalCode':
          if (row.defectiveProductDisposalCode) {
            this.showDetailDisposalCode(row)
          }
          break
        case 'purchaseOrderCode':
          if (row.purchaseOrderCode) {
            this.showDetailOrderCode(row)
          }
          break
      }
    },
    getList() {
      list(this.pageData).then(res => {
        res.data.data.data.forEach((item) => { item.isNotselected = true })
        this.tableData = res.data.data.data
        this.total = res.data.data.total
        this.loading = false
        // 合计字段
        this.totalList = res.data
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
          this.$refs.table.doLayout()
        })
      })
    },
    getEquipmentStatusUnit() {
      getDictSelects('materialUnitId').then(res => {
        this.equipmentList1 = res.data
      })
    },
    // 导出
    getExportData(data) {
      return new Promise(function(resolve, reject) {
        listForExport(data).then(res => {
          resolve({ data: res.data.data.data })
        })
      })
    },
    showDetailOrderCode(row) {
      OrderCodeInfo(row.purchaseOrderId).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetailOrder, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.OrdershowKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看采购订单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    // 查看不良品详情
    showDetailDisposalCode(row) {
      rejectsDisposeInfo(row.defectiveProductDisposalId).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetailDisposalCode, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showDisposalCodeKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看不良品处置单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    // 查看退货单号
    showDetail(row) {
      getInfo(row.purchaseReturnId).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetail, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看采购退货单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    // 合计行
    getSummaries(param) {
      const {
        columns
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        switch (column.property) {
          case 'purchaseNumber':
            sums[index] = this.totalList.purchaseNumberSum
            break
        }
        switch (column.property) {
          case 'returnNumber':
            sums[index] = this.totalList.alreadyReturnNumberSum
            break
        }
      })
      return sums
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-tooltip{
  display: flex !important;
}
.adequate_space{
  color: #19BE6B;
  background-color:rgb(223, 249, 241) ;
  border-radius: 3px;
  padding: 3px 0;
  width: 80px;
  text-align: center;
}
.insufficient_space{
  color: rgb(231,108,108);
  background-color:rgb(253,238,238) ;
  border-radius: 3px;
  padding: 3px 0;
  width: 80px;
  text-align: center;
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: rgba(0,0,0,0);
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: transparent!important;
}
/deep/ .el-table th.el-table__cell.is-leaf, .el-table td.el-table__cell {
  border-bottom: 1px solid #dfe6ec;
}

/deep/ .SaleOrderCodeColumnClass_expand{
  display: inline-block;
  width: 20px;
  line-height: 20px;
  height: 20px;
  text-align: center;
  margin-right: 3px;
}
/deep/.el-table .displayNoneClass .cell .el-checkbox__inner{
  display: none
}
</style>
